﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace Rio.Manager.Application.Behaviors
{
    public class UnderlineTextBlock : Behavior<TextBlock>
    {
        #region Constructors

        public UnderlineTextBlock()
            : base()
        {
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Called when [attached].
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;
            AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
            AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp;
        }

        /// <summary>
        /// Called when [detaching].
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();

            AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;
            AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
            AssociatedObject.MouseLeftButtonUp -= AssociatedObject_MouseLeftButtonUp;
        }

        /// <summary>
        /// Activates the underline.
        /// </summary>
        private void ActivateUnderline()
        {
            AssociatedObject.TextDecorations = TextDecorations.Underline;
            AssociatedObject.Cursor = Cursors.Hand;
        }

        /// <summary>
        /// Handles the MouseEnter event of the AssociatedObject control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        private void AssociatedObject_MouseEnter(object sender, MouseEventArgs e)
        {
            ActivateUnderline();
        }

        /// <summary>
        /// Handles the MouseLeave event of the AssociatedObject control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        private void AssociatedObject_MouseLeave(object sender, MouseEventArgs e)
        {
            DesactivateUnderline();
        }

        /// <summary>
        /// Handles the MouseLeftButtonUp event of the AssociatedObject control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            DesactivateUnderline();
        }

        /// <summary>
        /// Desactivates the underline.
        /// </summary>
        private void DesactivateUnderline()
        {
            AssociatedObject.TextDecorations = null;
            AssociatedObject.Cursor = Cursors.Arrow;
        }

        #endregion Methods
    }
}